<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="网址名称" prop="linkName">
        <el-input v-model="queryParams.linkName" placeholder="请输入网址名称" clearable size="small" style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh-right" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single"  @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDel">删除</el-button>
      </el-col>

      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="40" />
      <el-table-column label="网址分类" prop="linkType" width="180" />
      <el-table-column label="网址名称" prop="linkName" width="180" />
      <el-table-column label="网址" prop="linkUrl" min-width="280" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="170" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />


    <el-dialog :title="title" :visible.sync="open" width="550px">
      <div>
        <el-row>
          <el-form ref="form" :model="form" :rules="rules" label-width="130px">
            <el-form-item label="网址类型" prop="linkType" class="form-item-require-lable">
              <el-input v-model="form.linkType" placeholder="请输入网址类型" clearable />
            </el-form-item>
            <el-form-item label="网址名称" prop="linkName" class="form-item-require-lable">
              <el-input v-model="form.linkName" placeholder="请输入网址名称" clearable />
            </el-form-item>
            <el-form-item label="网址" prop="linkUrl">
              <el-input v-model="form.linkUrl" placeholder="请输入网址" clearable />
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保存
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {
    listLink,
    getLink,
    addLink,
    editLink,
    delLink
  } from "@/api/monitor/nav";

  export default {
    name: "Link",
    data() {
      return {
        // 遮罩层
        loading: false,
        tabopen1: true,
        tabopen2: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 角色表格数据
        roleList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示弹出层（数据权限）
        openDataScope: false,
        // 状态数据字典
        statusOptions: [],
        fileTrademark: [],
        // 菜单列表
        menuOptions: [],
        // 机构列表
        deptOptions: [],
        // 查询参数
        queryParams: {
          current: 1,
          size: 10,
          code: undefined,
          status: undefined
        },
        // 表单校验
        rules: {
          linkType: [{
            required: true,
            message: "网址分类不能为空",
            trigger: "blur"
          }],
          linkName: [{
            required: true,
            message: "网址名称不能为空",
            trigger: "blur"
          }],
          linkUrl: [{
            required: true,
            message: "网址不能为空",
            trigger: "blur"
          }],
        },
        // 显示搜索条件
        showSearch: true,
        // 表单参数
        form: {},
        steps: [{
          title: "基本信息"
        }, {
          title: "上传图片"
        }],
        goodsId: undefined,
        active: 0,
        imglist: []
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        this.loading = true;
        listLink(this.queryParams).then(
          response => {
            this.roleList = response.data;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      // 表单重置
      reset() {
        if (this.$refs.menu != undefined) {
          this.$refs.menu.setCheckedKeys([]);
        }
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.current = 1;
        this.getList();
      },
      handleAdd() {
        this.open = true;
      },
      cancel() {
        this.open = false;
        this.title="新建网址"
      },
      /** 修改按钮操作 */
      handleEdit(row) {
        console.log(row);
        this.reset();
        const id = row.id || this.ids
        getLink(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改网址";
        });
      },
      handleDel(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除ID为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLink(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      fileAuth() {},
      resetQuery() {
        this.resetForm("queryForm");
      },
      submitForm() {
        this.$refs["form"].validate(valid => {
          this.loading = true;
          if (this.form.id == null) {
            addLink(this.form).then(
              response => {
                if (response.code == 0) {
                  this.loading = false;
                  this.msgSuccess("保存成功");
                  this.cancel();
                  this.getList();
                  this.reset();
                } else {
                  this.loading = false;
                  this.msgError(response.msg);
                }
              }
            );
          }else{
            editLink(this.form).then(
              response => {
                if (response.code == 0) {
                  this.loading = false;
                  this.msgSuccess("修改成功");
                  this.cancel();
                  this.getList();
                  this.reset();
                } else {
                  this.loading = false;
                  this.msgError(response.msg);
                }
              }
            );
          }
        });
      }
    }
  };
</script>
